package com.blogspot.himynameisthirdy.blinders;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * WarriorPig project
 * @since 5/4/09
 * @author thirdy
 * http://thirdy.co.nr
 */
public class Main extends JFrame {

    private static boolean verbose = false;
    private static TextTransfer textTransfer = new TextTransfer();
    private static String raw;
    private JTextArea textArea = null;
    private JComboBox langFromComboBox = null;
    private JComboBox langToComboBox = null;
    private Language langTo, langFrom;

    public static void main(String[] args) {
        raw = textTransfer.getClipboardContents();
        Translate.setHttpReferrer("http://testing.com");
        // CLI
        if (args.length > 0) {
            raw = args[0];
            try {
                translate(raw,  Language.ENGLISH, Language.FILIPINO);
            } catch (Exception ex) {
                out("Error occured: " + ex.getMessage());
            }
            System.exit(0);
        }
        // GUI
        try {
            new Main();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error occured: " + ex.getMessage());
        }
    }

    public Main() throws Exception {
        super("WarriorPig 0.1");
//        if (raw != null && !raw.isEmpty()) {
//            textArea = new JTextArea(translate(raw, Language.ENGLISH, Language.FILIPINO), 5, 5);
//        }else{
            textArea = new JTextArea(5, 5);
//        }
        JButton translateButton = new JButton("Translate");
        JButton helpButton = new JButton("?");
        langFromComboBox = new JComboBox(Language.values());
        langFromComboBox.setSelectedItem(Language.FILIPINO);
        langToComboBox = new JComboBox(Language.values());
        langToComboBox.setSelectedItem(Language.ENGLISH);
        // button actions
        translateButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
//                    textArea.setText(translate(textArea.getText(), Language.ENGLISH, Language.FILIPINO));
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "Error occured: " + ex.getMessage());
                }
            }
        });

        helpButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"<html>Programmed by: Thirdy" +
                        "<br> blog: <a href='http:\\\\himynameisthirdy.blogspot.com'>himynameisthirdy.blogspot.com</a>" +
                        "<br> site: <a href='http:\\\\thirdy.co.nr'>thirdy.co.nr</a>" +
                        "<br> email: thirdy.derivera@gmail.com" +
                        "<br> version: 0.1" +
                        "<br></html>");
            }
        });

        // combo box actions
        langFromComboBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                langFrom = (Language) langFromComboBox.getSelectedItem();
            }
        });

        langToComboBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                langTo = (Language) langToComboBox.getSelectedItem();
            }
        });

        // window action
        addWindowFocusListener(new WindowAdapter() {

            @Override
            public void windowGainedFocus(WindowEvent e) {
                try {
                    textArea.setText(translate(textTransfer.getClipboardContents(), Language.ENGLISH, Language.FILIPINO));
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "Error occured: " + ex.getMessage());
                }
            }

        });
        BufferedImage icon = null;
        try {
            icon = ImageIO.read(new File("res/Warrior_Pig.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(icon);
        JPanel controlPanel = new JPanel(new FlowLayout());
        controlPanel.add(langFromComboBox);
        controlPanel.add(langToComboBox);
        controlPanel.add(translateButton);
        controlPanel.add(helpButton);
        Container c = getContentPane();
        c.setLayout(new BorderLayout(5,5));
        c.add(textArea, BorderLayout.CENTER);
        c.add(controlPanel,BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private static String translate(String s, Language from, Language to) throws Exception {
        String translatedText;
        if (verbose) {
            out("Text to translate:");
            out(s);
        }
        translatedText = Translate.translate(s, from, to);
        if (verbose) {
            out("Traslated Text:");
            out(translatedText);
        }
        return translatedText;
    }

    private static void printlnLangs() {
        for (Language lang : Arrays.asList(Language.values()) ) {
            out(lang.toString());
        }
    }

    private static void out(String s) {
        System.out.println(s);
    }
}
